QUẢN LÝ NHÂN – HỘ KHẨU PHƯỜNG XÃ, QUẬN HUYỆN
- MyDBengine.java
- project /
1 /* Ky thuat serialization copyychang form VoVanHai's blog
2 */
3
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.ObjectInputStream;
8 import java.io.ObjectOutputStream;
9
10 public class MyDBengine {
11 /**
12 * Serial 1 doi tuong xuong file
13 * @param fileName file ch? dinh
14 * @param h: doi tuong can serial
15 * @throws Exception
16 */
17 public static void SerialObject(String fileName, Object obj) throws IOException{
18 //Tao luong ghi file
19 FileOutputStream fs=new FileOutputStream(fileName);
20 //Tao luong de serial doi tuong
21 ObjectOutputStream os=new ObjectOutputStream(fs);
22 //chuyen tai doi tuong toi dich (file)
23 os.writeObject(obj);
24 //dong luon
25 fs.close();os.close();
26 }
27 /**
28 * Khôi phuc(deserial) 1 doi tuong da duoc serial truoc do len bo nho.
29 * @param fileName: file ch? dinh
30 * @return doi tuong da duoc phuc hoi
31 * @throws Exception
32 */
33 public static Object DeserialObject(String fileName) throws Exception{
34 Object kp=null;
35 //Tao luong doc file da duoc serial
36 FileInputStream fi=new FileInputStream(fileName);
37 //Tao luong de Deserialize doi tuong
38 ObjectInputStream ois=new ObjectInputStream(fi);
39 //Tien hành khôi phuc doi tuong
40 kp=ois.readObject();
41 //dóng luong
42 fi.close();ois.close();
43 return kp;
44 }
45 public static void main (String[] args) throws Exception{
46 try{
47 Object a=new NguoiTrongNha();
48 System.out.println ("ok1");
49 SerialObject("nguoi.dat",a);
50 System.out.println ("ok2");
51 Object o=DeserialObject("nguoi.dat");
52 System.out.println ("ok3");
53 System.out.println (o);
54 System.out.println ("ok4");
55 }
56 catch(Exception e){
57 System.out.println (e);
58 }
59 }
60 }
2 */
3
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.ObjectInputStream;
8 import java.io.ObjectOutputStream;
9
10 public class MyDBengine {
11 /**
12 * Serial 1 doi tuong xuong file
13 * @param fileName file ch? dinh
14 * @param h: doi tuong can serial
15 * @throws Exception
16 */
17 public static void SerialObject(String fileName, Object obj) throws IOException{
18 //Tao luong ghi file
19 FileOutputStream fs=new FileOutputStream(fileName);
20 //Tao luong de serial doi tuong
21 ObjectOutputStream os=new ObjectOutputStream(fs);
22 //chuyen tai doi tuong toi dich (file)
23 os.writeObject(obj);
24 //dong luon
25 fs.close();os.close();
26 }
27 /**
28 * Khôi phuc(deserial) 1 doi tuong da duoc serial truoc do len bo nho.
29 * @param fileName: file ch? dinh
30 * @return doi tuong da duoc phuc hoi
31 * @throws Exception
32 */
33 public static Object DeserialObject(String fileName) throws Exception{
34 Object kp=null;
35 //Tao luong doc file da duoc serial
36 FileInputStream fi=new FileInputStream(fileName);
37 //Tao luong de Deserialize doi tuong
38 ObjectInputStream ois=new ObjectInputStream(fi);
39 //Tien hành khôi phuc doi tuong
40 kp=ois.readObject();
41 //dóng luong
42 fi.close();ois.close();
43 return kp;
44 }
45 public static void main (String[] args) throws Exception{
46 try{
47 Object a=new NguoiTrongNha();
48 System.out.println ("ok1");
49 SerialObject("nguoi.dat",a);
50 System.out.println ("ok2");
51 Object o=DeserialObject("nguoi.dat");
52 System.out.println ("ok3");
53 System.out.println (o);
54 System.out.println ("ok4");
55 }
56 catch(Exception e){
57 System.out.println (e);
58 }
59 }
60 }